package io.gitee.kotle.android.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.gitee.kotle.android.widget.BaseRecyclerView
import io.gitee.kotle.android.widget.BaseTextView


/**
 *
 * 描述：IndexRecyclerView
 *
 * 创建人：jiale.wei
 * 创建时间：2022/2/10 5:59 下午
 *
 */
class IndexRecyclerView : BaseRecyclerView {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )


    init {
        layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        overScrollMode = View.OVER_SCROLL_NEVER
    }

    override fun onTouchEvent(e: MotionEvent?): Boolean {
        when (e?.action) {
            MotionEvent.ACTION_DOWN -> {
                lastHolder = null
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
            }
        }
        checkEvent(e)
        return true
    }

    /**
     * 记录holder防止多次重复回调
     */
    private var lastHolder: ViewHolder? = null

    /**
     * 设置监听器
     */
    var onIndexListener: Function1<ViewHolder, Unit>? = null

    /**
     * 设置简单的文本
     */
    fun setSimpleData(
        datas: List<String>,
        onViewListener: Function2<BaseTextView, ViewGroup, TextView?>? = null
    ) {
        adapter = object : RecyclerView.Adapter<ViewHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
                val textView = BaseTextView(parent.context)
                val view = onViewListener?.invoke(textView, parent) ?: textView
                return object : ViewHolder(view) {}
            }

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                (holder.itemView as TextView).text = datas[position]
            }

            override fun getItemCount(): Int = datas.count()
        }
    }

    private fun checkEvent(e: MotionEvent?) {
        e ?: return
        val child = findChildViewUnder(checkX(e.x), checkY(e.y)) ?: return
        val holder = findContainingViewHolder(child)
        if (holder != null && lastHolder != holder) {
            lastHolder = holder
            onIndexListener?.invoke(holder)
        }
    }

    private fun checkX(float: Float): Float {
        var new = float
        val start = paddingStart.toFloat()
        if (new <= start) {
            new = start + 1f
        }
        val end = width.toFloat() - paddingEnd
        if (new >= end) {
            new = end - 1f
        }
        return new
    }

    private fun checkY(float: Float): Float {
        var new = float
        val start = paddingTop.toFloat()
        if (new <= start) {
            new = start + 1f
        }
        val end = height.toFloat() - paddingBottom
        if (new >= end) {
            new = end - 1f
        }
        return new
    }
}